#ifndef AUTO_CLIENT_H
#define AUTO_CLIENT_H

#include <QJsonObject>
#include <QMap>
#include <QList>
#include <QTimer>
#include <QStringList>
#include "msgsocket.h"

class AutoClient: public QObject
{
    Q_OBJECT
signals:
    void signalUserOnline(UserInfo info);
    void signalUserOffline(UserInfo info);

public slots:
    // 状态，上线下线状态
    void SltTcpStatus(const quint8 &state);
    void SltTcpReply(const quint8 &type, const QJsonValue &dataVal);

public:
    AutoClient(QObject *parent = NULL);
    void connectToServer(const QString &host, const int &port);

    UserInfoList m_friendList;
    void autoChatWithFriend(void);

    void userRegisterAskMsg(const QString &name, const QString &passwd);
    void userLoginAskMsg(const QString &name, const QString &passwd);
    void userGetFriendsAskMag(void);
    void userAddFriendAskMsg(const QString &f_name);
    void userChatWithFriend(const QString &name, const QString &text);

protected:
    // 以下便是每个消息类型的解析
    void ParseUpFriendHead(const QJsonValue &dataVal);
    void ParseAddFriendReply(const QJsonValue &dataVal);
    void ParseAddFriendRequest(const QJsonValue &dataVal);
    void ParseGetFriendsReply(const QJsonValue &dataVal);
    void ParseRefreshFriendsReply(const QJsonValue &dataVal);
    void ParseFriendMessageReply(const QJsonValue &dataVal);

private:
    MsgSocket *m_msgSocket;
    static QStringList m_chatMsgList;

    // 头像图片的获取
    QString GetHeadPixmap(const QString &name) const;
    void DownloadFriendHead(const int &userId, const QString &strHead);
};

typedef QMap<QString, AutoClient*> ClientMap; //name, client

#endif // AUTO_CLIENT_H
